#!/bin/bash

# ============================================================
# OHOS 测试服务器 - Docker 镜像构建脚本
# ============================================================

set -e

# 颜色定义
RESET='\033[0m'
GREEN='\033[32m'
YELLOW='\033[33m'
CYAN='\033[36m'

echo -e "${CYAN}========================================${RESET}"
echo -e "${CYAN}  构建 OHOS 测试服务器 Docker 镜像${RESET}"
echo -e "${CYAN}========================================${RESET}"
echo ""

# 镜像信息
IMAGE_NAME="ohos-test-servers"
IMAGE_TAG="latest"
FULL_IMAGE_NAME="${IMAGE_NAME}:${IMAGE_TAG}"

echo -e "${CYAN}📦 镜像名称: ${FULL_IMAGE_NAME}${RESET}"
echo -e "${CYAN}📁 构建上下文: $(pwd)${RESET}"
echo ""

# 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${YELLOW}❌ Docker 未安装，请先安装 Docker${RESET}"
    exit 1
fi

echo -e "${CYAN}🔨 开始构建镜像...${RESET}"
echo ""

# 构建镜像
docker build -t "${FULL_IMAGE_NAME}" . \
    --build-arg BUILD_DATE="$(date -u +'%Y-%m-%dT%H:%M:%SZ')" \
    --build-arg VERSION="1.0.0"

if [ $? -eq 0 ]; then
    echo ""
    echo -e "${GREEN}========================================${RESET}"
    echo -e "${GREEN}✅ 镜像构建成功！${RESET}"
    echo -e "${GREEN}========================================${RESET}"
    echo ""
    echo -e "${CYAN}📋 镜像信息:${RESET}"
    docker images "${FULL_IMAGE_NAME}" --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}\t{{.CreatedAt}}"
    echo ""
    echo -e "${CYAN}🚀 启动容器:${RESET}"
    echo -e "   ./start.container.sh"
    echo ""
    echo -e "${CYAN}🔍 查看镜像详情:${RESET}"
    echo -e "   docker inspect ${FULL_IMAGE_NAME}"
    echo ""
else
    echo -e "${YELLOW}❌ 镜像构建失败${RESET}"
    exit 1
fi